package com.vegetable.modules.service.baseData.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.vegetable.modules.entity.baseData.AccountDetails;
import com.vegetable.modules.entity.baseData.po.AccountDetailsQueryPO;
import com.vegetable.modules.entity.finance.po.ADCanPayPO;
import com.vegetable.modules.entity.finance.vo.PaymentAgentStatisticsVO;
import com.vegetable.modules.entity.inbound.PackDelivery;
import com.vegetable.modules.mapper.baseData.AccountDetailsMapper;
import com.vegetable.modules.service.baseData.IAccountDetailsService;
import com.vegetable.modules.service.inbound.IPackDeliveryService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author wangruijie
 * @since 2024-03-21
 */
@Slf4j
@Service
public class AccountDetailsServiceImpl extends ServiceImpl<AccountDetailsMapper, AccountDetails> implements IAccountDetailsService {
    @Resource
    private IPackDeliveryService iPackDeliveryService;
    /**
     * 往来明细列表查询
     * @param po
     * @return
     */
    @Override
    public IPage queryPage(AccountDetailsQueryPO po) {
        IPage<AccountDetails> page = this.page(
                new Page<>(po.getPage(), po.getSize()),
                new QueryWrapper<AccountDetails>()
                        .between(ObjectUtils.isNotNull(po.getStartTime()), "order_time", po.getStartTime(), po.getEndTime())
                        .eq(ObjectUtils.isNotNull(po.getStaffType()),"staff_type", po.getStaffType())
                        .eq(ObjectUtils.isNotNull(po.getDetailType()),"detail_type", po.getDetailType())
                        .eq(ObjectUtils.isNotNull(po.getPersonId()),"person_id", po.getPersonId())
                        .orderByDesc("order_time")
        );
        return page;
    }
    @Override
    public IPage<PaymentAgentStatisticsVO> teamStatistics(IPage page, ADCanPayPO po){
     return baseMapper.teamStatistics(page, po);
    }

    @Override
    public IPage canPaymentList(AccountDetailsQueryPO po) {
        IPage<AccountDetails> page = this.page(
                new Page<>(po.getPage(), po.getSize()),
                new QueryWrapper<AccountDetails>()
                        .between(ObjectUtils.isNotNull(po.getStartTime()), "order_time", po.getStartTime(), po.getEndTime())
                        .eq(ObjectUtils.isNotNull(po.getStaffType()),"staff_type", po.getStaffType())
                        .eq(ObjectUtils.isNotNull(po.getDetailType()),"detail_type", po.getDetailType())
                        .eq(ObjectUtils.isNotNull(po.getPersonId()),"person_id", po.getPersonId())
                        .eq("is_pay_finish", 0)
                        .orderByDesc("order_time")
        );
        List<AccountDetails> detailsList = page.getRecords();
        if(CollectionUtil.isNotEmpty(detailsList) && po.getShowDetail() && ObjectUtils.isNotNull(po.getStaffType())){
            List<Long> orderIdList = detailsList.stream().map(el -> el.getOrderId()).collect(Collectors.toList());
            switch (po.getStaffType()){
                case 3:
                    List<PackDelivery> packDeliveryList = iPackDeliveryService.selectByIds(orderIdList);
                    for(AccountDetails ad : detailsList){
                        PackDelivery currentPD = packDeliveryList.stream().filter(el -> el.getId().equals(ad.getOrderId())).findAny().orElse(null);
                        if(ObjectUtil.isNotNull(currentPD)){
                            ad.setCommodityName(currentPD.getFinishCommodityName());
                            ad.setPlanWeight(currentPD.getFinishWeight());
                            ad.setPlanNumber(currentPD.getFinishNumber());
                            ad.setActualPay(ad.getMoney());

                        }
                    }
                    break;
                default:
                    break;
            }
        }
        return page;
    }

}
